package org.erp.admins.controller;

import org.erp.admins.infrastructure.convert.StorageChannelsConvert;
import org.erp.admins.domain.bo.StorageChannelsBo;
import org.erp.admins.domain.dto.StorageChannelsDto;
import org.erp.admins.domain.vo.StorageChannelsVo;
import org.erp.admins.service.StorageChannelsService;
import org.erp.common.page.DataPageResp;
import org.erp.common.page.PageQueryModel;
import org.erp.common.domain.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

import static org.erp.common.domain.Result.success;
/**
 * 存储渠道管理
 */
@RestController
@RequestMapping("/storage/channels")
public class StorageChannelsController {
    @Resource
    private StorageChannelsService storageChannelsService;

    @Resource
    private StorageChannelsConvert storageChannelsConvert;

    /**
     * 分页获取列表
     * @param bo
     * @return
     */
    @GetMapping("/getListByPage")
    public Result<DataPageResp<StorageChannelsVo>> getListByPage(StorageChannelsBo bo) {
        StorageChannelsDto dto = storageChannelsConvert.boConvertDto(bo);
        PageQueryModel<StorageChannelsVo> qm = storageChannelsService.getListByPage(dto);
        return success(DataPageResp.of(qm.getTotal(), qm.getDataList()));
    }
    /**
     * 获取列表
     * @param bo
     * @return
     */
    @GetMapping("/getList")
    public Result<List<StorageChannelsVo>> getList(StorageChannelsBo bo) {
        StorageChannelsDto dto = storageChannelsConvert.boConvertDto(bo);
        return Result.success(storageChannelsService.getStorageChannelsVoList(dto));
    }
    /**
     * 获取详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public Result<StorageChannelsVo> selectOne(@PathVariable Long id) {
        return Result.success(storageChannelsService.getStorageChannelsVoById(id));
    }
    /**
     * 新增存储渠道
     * @param dto
     * @return
     */
    @PostMapping("/add")
    public Result<Boolean> insert(@RequestBody StorageChannelsDto dto) {
        boolean result = storageChannelsService.addStorageChannels(dto);
        return result ? Result.success("新增成功") : Result.error("新增失败");
    }
    /**
     * 修改存储渠道
     * @param dto
     * @return
     */

    @PostMapping("/upd")
    public Result<Boolean> update(@RequestBody StorageChannelsDto dto) {
        boolean result = storageChannelsService.updStorageChannels(dto);
        return result ? Result.success("修改成功") : Result.error("修改失败");
    }
    /**
     * 删除存储渠道
     * @param bo
     * @return
     */
    @PostMapping("/del")
    public Result<Boolean> delete(@RequestBody StorageChannelsBo bo) {
        boolean result = storageChannelsService.removeById(bo.getId());
        return result ? Result.success("删除成功") : Result.error("删除失败");
    }
}

